﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Infrastructure;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class MembershipUserEmployeeDao : AbstractDao<ViewMembershipUserEmployee>, IMembershipUserEmployeeDao
    {
        public bool
        updateAccountDetails(string empId, string fname, string lname, bool isMale)
        {
            try
            {
                var q = (from e in _ctx.Employees
                         where e.empId == empId
                         select e).First();
                q.empFirstName = fname;
                q.empLastName = lname;
                q.empGender = isMale;
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override ViewMembershipUserEmployee
        getById(object id)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public PagingObject<ViewMembershipUserEmployee>
        getAll(int page)
        {
            try
            {
                var q = from e in _ctx.ViewMembershipUserEmployees
                        select e;

                List<ViewMembershipUserEmployee> list = new List<ViewMembershipUserEmployee>();
                int numberOfObjectsPerPage = 10;
                var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                PagingObject<ViewMembershipUserEmployee> obj = new PagingObject<ViewMembershipUserEmployee>();
                obj.Data = queryResultPage.ToList();
                obj.TotalPage = q.Count() / numberOfObjectsPerPage;
                if (q.Count() % numberOfObjectsPerPage > 0)
                    obj.TotalPage = obj.TotalPage + 1;
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ViewMembershipUserEmployee
        getAccountDetails(object userId)
        {
            try
            {
                var q = from e in _ctx.ViewMembershipUserEmployees
                        where e.UserId.Equals(new Guid(userId.ToString()))
                        select e;
                return q.SingleOrDefault();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ViewMembershipUserEmployee
        getUser(string username)
        {
            try
            {
                var q = from e in _ctx.ViewMembershipUserEmployees
                        where e.UserName == username
                        select e;
                ViewMembershipUserEmployee mue = q.SingleOrDefault();
                return mue;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public PagingObject<ViewMembershipUserEmployee>
        searchEmpHaveAcc(string searchBy, string key, int page)
        {
            try
            {
                int numberOfObjectsPerPage = 10;
                int totalRecord = 0;
                PagingObject<ViewMembershipUserEmployee> obj = new PagingObject<ViewMembershipUserEmployee>();
                if (searchBy == "empId")
                {
                    var q = from e in _ctx.ViewMembershipUserEmployees
                            where e.empId.Contains(key)
                            select e;
                    var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                    obj.Data = queryResultPage.ToList();
                    totalRecord = q.Count();
                }
                else if (searchBy == "name")
                {
                    var q = from e in _ctx.ViewMembershipUserEmployees
                            where (e.empFirstName + " " + e.empLastName).Contains(key) || e.UserName.Contains(key)
                            select e;
                    var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                    obj.Data = queryResultPage.ToList();
                    totalRecord = q.Count();
                }

                obj.TotalPage = totalRecord / numberOfObjectsPerPage;
                if (totalRecord % numberOfObjectsPerPage > 0)
                    obj.TotalPage = obj.TotalPage + 1;
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public PagingObject<Employee>
        searchEmpNoAcc(string searchBy, string key, int page)
        {
            try
            {
                int numberOfObjectsPerPage = 10;
                int totalRecord = 0;
                PagingObject<Employee> obj = new PagingObject<Employee>();
                if (searchBy == "empId")
                {
                    var q = from e in _ctx.Employees
                            where e.empId.Contains(key) && e.userId.Value == null
                            select e;
                    var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                    obj.Data = queryResultPage.ToList();
                    totalRecord = q.Count();
                }
                else if (searchBy == "name")
                {
                    var q = from e in _ctx.Employees
                            where (e.empFirstName + " " + e.empLastName).Contains(key) && e.userId.Value == null
                            select e;
                    var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                    obj.Data = queryResultPage.ToList();
                    totalRecord = q.Count();
                }

                obj.TotalPage = totalRecord / numberOfObjectsPerPage;
                if (totalRecord % numberOfObjectsPerPage > 0)
                    obj.TotalPage = obj.TotalPage + 1;
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IMembershipUserEmployeeDao

    public interface IMembershipUserEmployeeDao : IDao<ViewMembershipUserEmployee>
    {
        bool updateAccountDetails(String empId, String fname, String lname, Boolean isMale);

        PagingObject<ViewMembershipUserEmployee> getAll(int page);

        ViewMembershipUserEmployee getAccountDetails(object userId);

        ViewMembershipUserEmployee getUser(string username);

        PagingObject<ViewMembershipUserEmployee> searchEmpHaveAcc(string searchBy, string key, int page);

        PagingObject<Employee> searchEmpNoAcc(string searchBy, string key, int page);
    }
    #endregion
}